using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    class HWScriptSignalCheck : HWScriptCommand
    {
        SignalView oSignalView;
        string sValue;
        SignalFormat eSignalFormat;

        public HWScriptSignalCheck(RapidHardware poRapidHardware, int piClockTime, string psSignalName, string psValue, SignalFormat peSignalFormat)
            : base(poRapidHardware, piClockTime, 95)
        {
            oSignalView = oRapidHardware.SystemInterface.GetSignalView(psSignalName);
            sValue = psValue;
            eSignalFormat = peSignalFormat;
        }

        public HWScriptSignalCheck(RapidHardware poRapidHardware, int piClockTime, string psSignalName, string psValue)
            : base(poRapidHardware, piClockTime, 95)
        {
            oSignalView = oRapidHardware.SystemInterface.GetSignalView(psSignalName);
            sValue = psValue;
            eSignalFormat = oSignalView.SignalFormat;
        }

        public override string DoCommand()
        {
            oSignalView.SignalFormat = eSignalFormat;
            if (oSignalView.FormattedValue.Trim() != sValue.Trim())
            {
                string sError = "ERROR: expected " + oSignalView.Name + " = {" + sValue.Trim() + "} != {" + oSignalView.FormattedValue.Trim() + "}";
                return sError;
            }
            return "";
        }

        public override string ToVerilog()
        {
            string sBinaryValue = oSignalView.Width.ToString() + "'b" + Conversion.StringFormatToBinary(sValue, eSignalFormat, oSignalView.Width);
            return "if (" + oSignalView.NameHDL + " != " + sBinaryValue + ") $display(\"ERROR:" + ClockTime.ToString() + "," + oSignalView.NameHDL + " = %b, expecting " + sBinaryValue + "\"," + oSignalView.NameHDL + ")";            
        }

        public override string SignalNameHDL()
        {
            return oSignalView.NameHDL;
        }

        public override string SignalValue()
        {
            string sBinaryValue = Conversion.StringFormatToBinary(sValue, eSignalFormat, oSignalView.Width);
            return sBinaryValue;
        }
    }
}

